﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HdipCricketClub.Models;
using HdipCricketClub.ViewModels;
using PagedList;

namespace HdipCricketClub.Controllers
{
    public class ScorecardController : Controller
    {
        // database context

        CricketClubEntities db = new CricketClubEntities();

        // repositories

        ClubFixturePlayerRepository clubFixturePlayerRepository = new ClubFixturePlayerRepository();
        FixtureRepository fixtureRepository = new FixtureRepository();
        MemberRepository memberRepository = new MemberRepository();
        OpponentFixturePlayerRepository opponentFixturePlayerRepository = new OpponentFixturePlayerRepository();
        ScorecardHeaderRepository scorecardHeaderRepository = new ScorecardHeaderRepository();

        // objects

        ClubFixturePlayer clubFixturePlayer = new ClubFixturePlayer();
        Fixture fixture = new Fixture();
        OpponentFixturePlayer opponentFixturePlayer = new OpponentFixturePlayer();
        Result result = new Result();
        ResultRepository resultRepository = new ResultRepository();
        ScorecardHeader scorecardHeader = new ScorecardHeader();
        ScorecardInputDataViewModel scorecardInputViewModel = new ScorecardInputDataViewModel();
        ScorecardEnterScoresViewModel scorecardEnterScoresViewModel = new ScorecardEnterScoresViewModel();
        ScorecardEditDataViewModel scorecardEditDataViewModel = new ScorecardEditDataViewModel();
        CompletedScorecardViewModel completedScorecardViewModel = new CompletedScorecardViewModel();
        ClubOtherFixtureDetail clubOtherFixtureDetail = new ClubOtherFixtureDetail();
        ClubOtherFixtureDetailRepository clubOtherFixtureDetailRepository = new ClubOtherFixtureDetailRepository();
        OpponentOtherFixtureDetail opponentOtherFixtureDetail = new OpponentOtherFixtureDetail();
        OpponentOtherFixtureDetailRepository opponentOtherFixtureDetailRepository = new OpponentOtherFixtureDetailRepository();
        PlayerSeasonStatistic playerSeasonStatistic = new PlayerSeasonStatistic();
        ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel = new ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel();
        //
        // GET: /Scorecard/

        public ViewResult Index()
        {
            var fixtures = fixtureRepository.FindFixturesWithNoScoreCard();

            if (fixtures.Count() < 1)
            {
                ViewBag.Message = "There are no fixtures with scorecards to be entered";
            }

            return View(fixtures.ToList());
        }

        //
        //  GET: /Scorecard/ResultType/

        public ViewResult ResultType(int id)
        {
            ScorecardResultTypeViewModel scorecardResultTypeViewModel = new ScorecardResultTypeViewModel();
            scorecardResultTypeViewModel.ScorecardFixture = fixtureRepository.GetFixture(id);
            scorecardResultTypeViewModel.ScorecardHeaderDetail = scorecardHeaderRepository.CreateNewScorecardHeader(scorecardResultTypeViewModel.ScorecardFixture);

            return View(scorecardResultTypeViewModel);
        }

        //
        //  POST:  /Scorecard/ResultType/

        [HttpPost]
        public ActionResult ResultType(ScorecardResultTypeViewModel scorecardResultTypeViewModel)
        {
            if (ModelState.IsValid)
            {
                // validate result type

                string errorMessage = result.Validate(scorecardResultTypeViewModel);

                if (errorMessage != "")
                {
                    ScorecardResultTypeViewModel reInputData = new ScorecardResultTypeViewModel();
                    reInputData.ScorecardFixture = fixtureRepository.GetFixture(scorecardResultTypeViewModel.ScorecardFixture.fixture_id);
                    reInputData.ScorecardHeaderDetail = scorecardResultTypeViewModel.ScorecardHeaderDetail;

                    ViewBag.ErrorMessage = errorMessage;
                    return View(reInputData);
                }

                if (scorecardResultTypeViewModel.ScorecardHeaderDetail.result_id == 1)
                {
                    return RedirectToAction("ConfirmResult", "ScorecardCalledOffFixture", new
                    {
                        fixtureId = scorecardResultTypeViewModel.ScorecardFixture.fixture_id,
                        resultId = scorecardResultTypeViewModel.ScorecardHeaderDetail.result_id
                    });
                }
                if (scorecardResultTypeViewModel.ScorecardHeaderDetail.result_id == 2)
                {
                    return RedirectToAction("EnterAbandonedFixtureDetail", "ScorecardAbandonedFixture", new
                    {
                        fixtureId = scorecardResultTypeViewModel.ScorecardFixture.fixture_id,
                        resultId = scorecardResultTypeViewModel.ScorecardHeaderDetail.result_id
                    });
                }
                if (scorecardResultTypeViewModel.ScorecardHeaderDetail.result_id == 3)
                {
                    return RedirectToAction("EnterCompletedFixtureDetail", "ScorecardCompletedFixture", new
                    {
                        fixtureId = scorecardResultTypeViewModel.ScorecardFixture.fixture_id,
                        resultId = scorecardResultTypeViewModel.ScorecardHeaderDetail.result_id
                    });
                }
            }
            return View(scorecardResultTypeViewModel);
        }

        //
        // GET: Scorecard/GetEditFixtures()

        public ActionResult GetEditFixtures()
        {
            var fixtures = fixtureRepository.FindFixtureScorecards();

            if (fixtures.Count() < 1)
            {
                ViewBag.Message = "There are no fixtures with scorecards to be entered";
            }

            return View(fixtures.ToList());
        }

        //
        // GET: Scorecard/GetDeleteFixtures()

        public ActionResult GetDeleteFixtures()
        {
            var fixtures = fixtureRepository.FindFixtureScorecards();

            if (fixtures.Count() < 1)
            {
                ViewBag.Message = "There are no fixtures with scorecards to be entered";
            }

            return View(fixtures.ToList());
        }
        
        //
        //  GET: Scorecard/GetEditScorecard

        public ActionResult GetEditScorecard(int id)
        {
            ScorecardBatAndBowlViewModel scorecardBatAndBowlViewModel = new ScorecardBatAndBowlViewModel();
            scorecardBatAndBowlViewModel.ScorecardFixture = fixtureRepository.GetFixture(id);
            scorecardBatAndBowlViewModel.ScorecardHeaderDetail = scorecardHeaderRepository.GetScorecardHeader(scorecardBatAndBowlViewModel.ScorecardFixture);

            if (scorecardBatAndBowlViewModel.ScorecardHeaderDetail.result_id == 1)                           // called off fixtures
            {
                return View();
            }

            if (scorecardBatAndBowlViewModel.ScorecardHeaderDetail.result_id == 2)                           // abandoned fixtures
            {
                if ((scorecardBatAndBowlViewModel.ScorecardHeaderDetail.batted) && (scorecardBatAndBowlViewModel.ScorecardHeaderDetail.bowled))
                {
                    scorecardBatAndBowlViewModel.ClubBatsmen = clubFixturePlayerRepository.GetClubBatsmen(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.ClubBowlers = clubFixturePlayerRepository.GetClubBowlers(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.ClubOtherDetail = clubOtherFixtureDetailRepository.GetClubOtherFixtureDetail(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.TotalClubBatsmenRuns = clubFixturePlayer.GetTotalBatsmenRuns(scorecardBatAndBowlViewModel.ClubBatsmen);
                    scorecardBatAndBowlViewModel.TotalClubExtras = clubOtherFixtureDetailRepository.GetTotalClubExtras(scorecardBatAndBowlViewModel.ClubOtherDetail);
                    scorecardBatAndBowlViewModel.OpponentBatsmen = opponentFixturePlayerRepository.GetOpponentBatsmen(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.OpponentBowlers = opponentFixturePlayerRepository.GetOpponentBowlers(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.OpponentOtherDetail = opponentOtherFixtureDetailRepository.GetOpponentOtherFixtureDetail(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.TotalOpponentBatsmenRuns = opponentFixturePlayer.GetTotalBatsmenRuns(scorecardBatAndBowlViewModel.OpponentBatsmen);
                    scorecardBatAndBowlViewModel.TotalOpponentExtras = opponentOtherFixtureDetailRepository.GetTotalOpponentExtras(scorecardBatAndBowlViewModel.OpponentOtherDetail);
                    scorecardBatAndBowlViewModel.DismissalTypes = db.DismissalTypes.ToList();

                    if (scorecardBatAndBowlViewModel.ScorecardHeaderDetail.batted_first)
                    {
                        return View("EditAbandonedScorecardBatAndBowlBattedFirst", scorecardBatAndBowlViewModel);
                    }
                    if (!scorecardBatAndBowlViewModel.ScorecardHeaderDetail.batted_first)
                    {
                        return View("EditAbandonedScorecardBatAndBowlBattedSecond", scorecardBatAndBowlViewModel);
                    }
                }

                if ((scorecardBatAndBowlViewModel.ScorecardHeaderDetail.batted) && (!scorecardBatAndBowlViewModel.ScorecardHeaderDetail.bowled))
                {
                    scorecardBatAndBowlViewModel.ClubBatsmen = clubFixturePlayerRepository.GetClubBatsmen(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.ClubOtherDetail = clubOtherFixtureDetailRepository.GetClubOtherFixtureDetail(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.TotalClubBatsmenRuns = clubFixturePlayer.GetTotalBatsmenRuns(scorecardBatAndBowlViewModel.ClubBatsmen);
                    scorecardBatAndBowlViewModel.TotalClubExtras = clubOtherFixtureDetailRepository.GetTotalClubExtras(scorecardBatAndBowlViewModel.ClubOtherDetail);
                    scorecardBatAndBowlViewModel.OpponentBatsmen = opponentFixturePlayerRepository.GetOpponentBatsmen(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.OpponentBowlers = opponentFixturePlayerRepository.GetOpponentBowlers(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.DismissalTypes = db.DismissalTypes.ToList();

                    return View("EditAbandonedScorecardBattedOnly", scorecardBatAndBowlViewModel);
                }
                if ((!scorecardBatAndBowlViewModel.ScorecardHeaderDetail.batted) && (scorecardBatAndBowlViewModel.ScorecardHeaderDetail.bowled))
                {
                    scorecardBatAndBowlViewModel.ClubBatsmen = clubFixturePlayerRepository.GetClubBatsmen(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.ClubBowlers = clubFixturePlayerRepository.GetClubBowlers(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.OpponentBatsmen = opponentFixturePlayerRepository.GetOpponentBatsmen(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.OpponentOtherDetail = opponentOtherFixtureDetailRepository.GetOpponentOtherFixtureDetail(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.TotalOpponentBatsmenRuns = opponentFixturePlayer.GetTotalBatsmenRuns(scorecardBatAndBowlViewModel.OpponentBatsmen);
                    scorecardBatAndBowlViewModel.TotalOpponentExtras = opponentOtherFixtureDetailRepository.GetTotalOpponentExtras(scorecardBatAndBowlViewModel.OpponentOtherDetail);
                    scorecardBatAndBowlViewModel.DismissalTypes = db.DismissalTypes.ToList();

                    return View("EditAbandonedScorecardBowledOnly", scorecardBatAndBowlViewModel);
                }
            }

            if (scorecardBatAndBowlViewModel.ScorecardHeaderDetail.result_id == 3)                           // completed fixtures
            {

                scorecardBatAndBowlViewModel.ClubBatsmen = clubFixturePlayerRepository.GetClubBatsmen(scorecardBatAndBowlViewModel.ScorecardFixture);
                scorecardBatAndBowlViewModel.ClubBowlers = clubFixturePlayerRepository.GetClubBowlers(scorecardBatAndBowlViewModel.ScorecardFixture);
                scorecardBatAndBowlViewModel.ClubOtherDetail = clubOtherFixtureDetailRepository.GetClubOtherFixtureDetail(scorecardBatAndBowlViewModel.ScorecardFixture);
                scorecardBatAndBowlViewModel.TotalClubBatsmenRuns = clubFixturePlayer.GetTotalBatsmenRuns(scorecardBatAndBowlViewModel.ClubBatsmen);
                scorecardBatAndBowlViewModel.TotalClubExtras = clubOtherFixtureDetailRepository.GetTotalClubExtras(scorecardBatAndBowlViewModel.ClubOtherDetail);
                scorecardBatAndBowlViewModel.OpponentBatsmen = opponentFixturePlayerRepository.GetOpponentBatsmen(scorecardBatAndBowlViewModel.ScorecardFixture);
                scorecardBatAndBowlViewModel.OpponentBowlers = opponentFixturePlayerRepository.GetOpponentBowlers(scorecardBatAndBowlViewModel.ScorecardFixture);
                scorecardBatAndBowlViewModel.OpponentOtherDetail = opponentOtherFixtureDetailRepository.GetOpponentOtherFixtureDetail(scorecardBatAndBowlViewModel.ScorecardFixture);
                scorecardBatAndBowlViewModel.TotalOpponentBatsmenRuns = opponentFixturePlayer.GetTotalBatsmenRuns(scorecardBatAndBowlViewModel.OpponentBatsmen);
                scorecardBatAndBowlViewModel.TotalOpponentExtras = opponentOtherFixtureDetailRepository.GetTotalOpponentExtras(scorecardBatAndBowlViewModel.OpponentOtherDetail);
                scorecardBatAndBowlViewModel.DismissalTypes = db.DismissalTypes.ToList();

                if (scorecardBatAndBowlViewModel.ScorecardHeaderDetail.batted_first)
                {
                    return View("EditCompletedFixtureBattedFirst", scorecardBatAndBowlViewModel);
                }
                if (!scorecardBatAndBowlViewModel.ScorecardHeaderDetail.batted_first)
                {
                    return View("EditCompletedFixtureBattedSecond", scorecardBatAndBowlViewModel);
                }
            }
            return View();
        }

        //
        //  GET: Scorecard/GetDeleteScorecard

        public ActionResult GetDeleteScorecard(int id)
        {
            ScorecardBatAndBowlViewModel scorecardBatAndBowlViewModel = new ScorecardBatAndBowlViewModel();
            scorecardBatAndBowlViewModel.ScorecardFixture = fixtureRepository.GetFixture(id);
            scorecardBatAndBowlViewModel.ScorecardHeaderDetail = scorecardHeaderRepository.GetScorecardHeader(scorecardBatAndBowlViewModel.ScorecardFixture);

            if (scorecardBatAndBowlViewModel.ScorecardHeaderDetail.result_id == 1)                           // called off fixtures
            {
                return View();
            }

            if (scorecardBatAndBowlViewModel.ScorecardHeaderDetail.result_id == 2)                           // abandoned fixtures
            {
                if ((scorecardBatAndBowlViewModel.ScorecardHeaderDetail.batted) && (scorecardBatAndBowlViewModel.ScorecardHeaderDetail.bowled))
                {
                    scorecardBatAndBowlViewModel.ClubBatsmen = clubFixturePlayerRepository.GetClubBatsmen(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.ClubBowlers = clubFixturePlayerRepository.GetClubBowlers(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.ClubOtherDetail = clubOtherFixtureDetailRepository.GetClubOtherFixtureDetail(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.TotalClubBatsmenRuns = clubFixturePlayer.GetTotalBatsmenRuns(scorecardBatAndBowlViewModel.ClubBatsmen);
                    scorecardBatAndBowlViewModel.TotalClubExtras = clubOtherFixtureDetailRepository.GetTotalClubExtras(scorecardBatAndBowlViewModel.ClubOtherDetail);
                    scorecardBatAndBowlViewModel.OpponentBatsmen = opponentFixturePlayerRepository.GetOpponentBatsmen(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.OpponentBowlers = opponentFixturePlayerRepository.GetOpponentBowlers(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.OpponentOtherDetail = opponentOtherFixtureDetailRepository.GetOpponentOtherFixtureDetail(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.TotalOpponentBatsmenRuns = opponentFixturePlayer.GetTotalBatsmenRuns(scorecardBatAndBowlViewModel.OpponentBatsmen);
                    scorecardBatAndBowlViewModel.TotalOpponentExtras = opponentOtherFixtureDetailRepository.GetTotalOpponentExtras(scorecardBatAndBowlViewModel.OpponentOtherDetail);
                    scorecardBatAndBowlViewModel.DismissalTypes = db.DismissalTypes.ToList();

                    if (scorecardBatAndBowlViewModel.ScorecardHeaderDetail.batted_first)
                    {
                        return View("DeleteAbandonedScorecardBatAndBowlBattedFirst", scorecardBatAndBowlViewModel);
                    }
                    if (!scorecardBatAndBowlViewModel.ScorecardHeaderDetail.batted_first)
                    {
                        return View("DeleteAbandonedScorecardBatAndBowlBattedSecond", scorecardBatAndBowlViewModel);
                    }
                }

                if ((scorecardBatAndBowlViewModel.ScorecardHeaderDetail.batted) && (!scorecardBatAndBowlViewModel.ScorecardHeaderDetail.bowled))
                {
                    scorecardBatAndBowlViewModel.ClubBatsmen = clubFixturePlayerRepository.GetClubBatsmen(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.ClubOtherDetail = clubOtherFixtureDetailRepository.GetClubOtherFixtureDetail(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.TotalClubBatsmenRuns = clubFixturePlayer.GetTotalBatsmenRuns(scorecardBatAndBowlViewModel.ClubBatsmen);
                    scorecardBatAndBowlViewModel.TotalClubExtras = clubOtherFixtureDetailRepository.GetTotalClubExtras(scorecardBatAndBowlViewModel.ClubOtherDetail);
                    scorecardBatAndBowlViewModel.OpponentBatsmen = opponentFixturePlayerRepository.GetOpponentBatsmen(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.OpponentBowlers = opponentFixturePlayerRepository.GetOpponentBowlers(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.DismissalTypes = db.DismissalTypes.ToList();

                    return View("DeleteAbandonedScorecardBattedOnly", scorecardBatAndBowlViewModel);
                }
                if ((!scorecardBatAndBowlViewModel.ScorecardHeaderDetail.batted) && (scorecardBatAndBowlViewModel.ScorecardHeaderDetail.bowled))
                {
                    scorecardBatAndBowlViewModel.ClubBatsmen = clubFixturePlayerRepository.GetClubBatsmen(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.ClubBowlers = clubFixturePlayerRepository.GetClubBowlers(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.OpponentBatsmen = opponentFixturePlayerRepository.GetOpponentBatsmen(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.OpponentOtherDetail = opponentOtherFixtureDetailRepository.GetOpponentOtherFixtureDetail(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.TotalOpponentBatsmenRuns = opponentFixturePlayer.GetTotalBatsmenRuns(scorecardBatAndBowlViewModel.OpponentBatsmen);
                    scorecardBatAndBowlViewModel.TotalOpponentExtras = opponentOtherFixtureDetailRepository.GetTotalOpponentExtras(scorecardBatAndBowlViewModel.OpponentOtherDetail);
                    scorecardBatAndBowlViewModel.DismissalTypes = db.DismissalTypes.ToList();

                    return View("DeleteAbandonedScorecardBowledOnly", scorecardBatAndBowlViewModel);
                }
            }

            if (scorecardBatAndBowlViewModel.ScorecardHeaderDetail.result_id == 3)                           // completed fixtures
            {

                scorecardBatAndBowlViewModel.ClubBatsmen = clubFixturePlayerRepository.GetClubBatsmen(scorecardBatAndBowlViewModel.ScorecardFixture);
                scorecardBatAndBowlViewModel.ClubBowlers = clubFixturePlayerRepository.GetClubBowlers(scorecardBatAndBowlViewModel.ScorecardFixture);
                scorecardBatAndBowlViewModel.ClubOtherDetail = clubOtherFixtureDetailRepository.GetClubOtherFixtureDetail(scorecardBatAndBowlViewModel.ScorecardFixture);
                scorecardBatAndBowlViewModel.TotalClubBatsmenRuns = clubFixturePlayer.GetTotalBatsmenRuns(scorecardBatAndBowlViewModel.ClubBatsmen);
                scorecardBatAndBowlViewModel.TotalClubExtras = clubOtherFixtureDetailRepository.GetTotalClubExtras(scorecardBatAndBowlViewModel.ClubOtherDetail);
                scorecardBatAndBowlViewModel.OpponentBatsmen = opponentFixturePlayerRepository.GetOpponentBatsmen(scorecardBatAndBowlViewModel.ScorecardFixture);
                scorecardBatAndBowlViewModel.OpponentBowlers = opponentFixturePlayerRepository.GetOpponentBowlers(scorecardBatAndBowlViewModel.ScorecardFixture);
                scorecardBatAndBowlViewModel.OpponentOtherDetail = opponentOtherFixtureDetailRepository.GetOpponentOtherFixtureDetail(scorecardBatAndBowlViewModel.ScorecardFixture);
                scorecardBatAndBowlViewModel.TotalOpponentBatsmenRuns = opponentFixturePlayer.GetTotalBatsmenRuns(scorecardBatAndBowlViewModel.OpponentBatsmen);
                scorecardBatAndBowlViewModel.TotalOpponentExtras = opponentOtherFixtureDetailRepository.GetTotalOpponentExtras(scorecardBatAndBowlViewModel.OpponentOtherDetail);
                scorecardBatAndBowlViewModel.DismissalTypes = db.DismissalTypes.ToList();

                if (scorecardBatAndBowlViewModel.ScorecardHeaderDetail.batted_first)
                {
                    return View("DeleteCompletedFixtureBattedFirst", scorecardBatAndBowlViewModel);
                }
                if (!scorecardBatAndBowlViewModel.ScorecardHeaderDetail.batted_first)
                {
                    return View("DeleteCompletedFixtureBattedSecond", scorecardBatAndBowlViewModel);
                }
            }
            return View();
        }


        //
        // POST: scorecard/UpdateScorecard

        [HttpPost]
        public ActionResult UpdateScores(FormCollection formCollection)
        {

            if (ModelState.IsValid)
            {
                Fixture fixture = fixtureRepository.ParseScorecardFormCollection(formCollection);
                ScorecardHeader scorecardHeader = scorecardHeaderRepository.ParseScorecardFormCollection(formCollection);

                if (scorecardHeader.result_id == 1)
                {
                    ScorecardHeader calledOffFixtureHeader = scorecardHeaderRepository.GetScorecardHeader(fixture.fixture_id);

                    db.DeleteObject(calledOffFixtureHeader);
                    db.SaveChanges();

                    return View("ConfirmEditCalledOffFixture");
                }

                if (scorecardHeader.result_id == 2)
                {
                    if ((scorecardHeader.batted) && (scorecardHeader.bowled))
                    {
                        if (scorecardHeader.batted_first)
                        {
                            ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel scorecardData = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ParseFormCollectionBattedFirst(formCollection);

                            // validate input

                            List<string> clubBatsmenErrorMessages = clubFixturePlayer.ValidateClubBatsmenScores(scorecardData.ClubBatsmen);
                            List<string> clubBowlersErrorMessages = clubFixturePlayer.ValidateClubBowlersScores(scorecardData.ClubBowlers);
                            List<string> clubOtherDetailsErrorMessages = clubOtherFixtureDetail.ValidateClubOtherDetails(scorecardData.ClubOtherDetail);
                            List<string> opponentBatsmenErrorMessages = opponentFixturePlayer.ValidateOpponentBatsmenScores(scorecardData.OpponentBatsmen);
                            List<string> opponentBowlersErrorMessages = opponentFixturePlayer.ValidateOpponentBowlersScores(scorecardData.OpponentBowlers);
                            List<string> opponentOtherDetailsErrorMessages = opponentOtherFixtureDetail.ValidateOpponentOtherDetails(scorecardData.OpponentOtherDetail);
                            List<string> scorecardClubErrorMessages = scorecardData.ValidateClubScorecardTotals(scorecardData);
                            List<string> scorecardOpponentErrorMessages = scorecardData.ValidateOpponentScorecardTotals(scorecardData);

                            if ((clubBatsmenErrorMessages.Count() == 0) && (clubBowlersErrorMessages.Count() == 0) && (opponentBatsmenErrorMessages.Count() == 0) && (opponentBowlersErrorMessages.Count() == 0) && (clubOtherDetailsErrorMessages.Count() == 0) && (opponentOtherDetailsErrorMessages.Count() == 0) && (scorecardClubErrorMessages.Count() == 0) && (scorecardOpponentErrorMessages.Count() == 0))
                            {
                                // update

                                clubFixturePlayer.EditClubFixtureBatsmen(scorecardData.ClubBatsmen);
                                clubFixturePlayer.EditClubFixtureBowlers(scorecardData.ClubBowlers);
                                opponentFixturePlayer.EditOpponentFixtureBatsmen(scorecardData.OpponentBatsmen);
                                opponentFixturePlayer.EditOpponentFixtureBowlers(scorecardData.OpponentBowlers);
                                clubOtherFixtureDetail.EditClubOtherFixtureDetail(scorecardData.ClubOtherDetail);
                                opponentOtherFixtureDetail.EditOpponentOtherFixtureDetail(scorecardData.OpponentOtherDetail);
                                                               

                                // create view model

                                ScorecardAbandonedFixtureScorecardBatAndBowlBattedFirst scorecard = new ScorecardAbandonedFixtureScorecardBatAndBowlBattedFirst();

                                scorecard.ScorecardFixture = fixtureRepository.GetFixture(scorecardData.ScorecardFixture.fixture_id);
                                scorecard.ScorecardHeaderDetail = scorecardHeaderRepository.GetScorecardHeader(scorecard.ScorecardFixture.fixture_id);
                                scorecard.ClubBatsmen = clubFixturePlayerRepository.GetClubBatsmen(scorecard.ScorecardFixture);
                                scorecard.ClubBowlers = clubFixturePlayerRepository.GetClubBowlers(scorecard.ScorecardFixture);
                                scorecard.ClubOtherDetail = clubOtherFixtureDetailRepository.GetClubOtherFixtureDetail(scorecard.ScorecardFixture.fixture_id);
                                scorecard.OpponentBatsmen = opponentFixturePlayerRepository.GetOpponentBatsmen(scorecard.ScorecardFixture);
                                scorecard.OpponentBowlers = opponentFixturePlayerRepository.GetOpponentBowlers(scorecard.ScorecardFixture);
                                scorecard.OpponentOtherDetail = opponentOtherFixtureDetailRepository.GetOpponentOtherFixtureDetail(scorecard.ScorecardFixture);
                                scorecard.TotalClubBatsmenRuns = clubFixturePlayer.GetTotalBatsmenRuns(scorecard.ClubBatsmen);
                                scorecard.TotalClubExtras = clubOtherFixtureDetailRepository.GetTotalClubExtras(scorecardData.ClubOtherDetail);
                                scorecard.TotalOpponentBatsmenRuns = opponentFixturePlayer.GetTotalBatsmenRuns(scorecard.OpponentBatsmen);
                                scorecard.TotalOpponentExtras = opponentOtherFixtureDetailRepository.GetTotalOpponentExtras(scorecardData.OpponentOtherDetail);

                                // update playerstatistics

                                playerSeasonStatistic.ResetStatistics();
                                playerSeasonStatistic.RegenerateStatistics();

                                return View("DisplayEditedAbandonedFixtureScorecardBatAndBowlBattedFirst", scorecard);
                            }
                            else
                            {
                                ScorecardAbandonedFixtureReInputScoresBatAndBowlViewModel reInputData = new ScorecardAbandonedFixtureReInputScoresBatAndBowlViewModel();

                                reInputData.ScorecardFixture = scorecardData.ScorecardFixture;
                                reInputData.ScorecardHeaderDetail = scorecardData.ScorecardHeaderDetail;
                                reInputData.ClubBatsmen = scorecardData.ClubBatsmen;
                                reInputData.OpponentBatsmen = scorecardData.OpponentBatsmen;
                                reInputData.ClubBowlers = scorecardData.ClubBowlers;
                                reInputData.OpponentBowlers = scorecardData.OpponentBowlers;
                                reInputData.ClubOtherDetail = scorecardData.ClubOtherDetail;
                                reInputData.OpponentOtherDetail = scorecardData.OpponentOtherDetail;
                                reInputData.DismissalTypes = db.DismissalTypes.ToList();
                                reInputData.ClubBatsmenErrorMessages = clubBatsmenErrorMessages;
                                reInputData.ClubBowlersErrorMessages = clubBowlersErrorMessages;
                                reInputData.OpponentBatsmenErrorMessages = opponentBatsmenErrorMessages;
                                reInputData.OpponentBowlersErrorMessages = opponentBowlersErrorMessages;
                                reInputData.ClubOtherDetailErrorMessages = clubOtherDetailsErrorMessages;
                                reInputData.OpponentOtherDetailErrorMessages = opponentOtherDetailsErrorMessages;
                                reInputData.ScorecardClubErrorMessages = scorecardClubErrorMessages;
                                reInputData.ScorecardOpponentErrorMessages = scorecardOpponentErrorMessages;
                                reInputData.TotalClubBatsmenRuns = clubFixturePlayer.GetTotalBatsmenRuns(reInputData.ClubBatsmen);
                                reInputData.TotalClubExtras = clubOtherFixtureDetail.GetTotalExtras(reInputData.ClubOtherDetail);
                                reInputData.TotalClubWicketsLost = clubFixturePlayer.GetTotalWicketsLost(reInputData.ClubBatsmen);
                                reInputData.TotalClubScore = reInputData.TotalClubBatsmenRuns + reInputData.TotalClubExtras;
                                reInputData.TotalOpponentBatsmenRuns = opponentFixturePlayer.GetTotalBatsmenRuns(reInputData.OpponentBatsmen);
                                reInputData.TotalOpponentExtras = opponentOtherFixtureDetail.GetTotalExtras(reInputData.OpponentOtherDetail);
                                reInputData.TotalOpponentWicketsLost = opponentFixturePlayer.GetTotalWicketsLost(reInputData.OpponentBatsmen);
                                reInputData.TotalOpponentScore = reInputData.TotalOpponentBatsmenRuns + reInputData.TotalOpponentExtras;

                                return View("ReInputEditAbandonedFixtureScoresBatAndBowlBattedFirst", reInputData);
                            }
                        }
                    }
                }
                if ((scorecardHeader.batted) && (scorecardHeader.bowled))
                {

                    if (!scorecardHeader.batted_first)
                    {
                        ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel scorecardData = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ParseFormCollectionBattedSecond(formCollection);

                        // validate input

                        List<string> clubBatsmenErrorMessages = clubFixturePlayer.ValidateClubBatsmenScores(scorecardData.ClubBatsmen);
                        List<string> clubBowlersErrorMessages = clubFixturePlayer.ValidateClubBowlersScores(scorecardData.ClubBowlers);
                        List<string> clubOtherDetailsErrorMessages = clubOtherFixtureDetail.ValidateClubOtherDetails(scorecardData.ClubOtherDetail);
                        List<string> opponentBatsmenErrorMessages = opponentFixturePlayer.ValidateOpponentBatsmenScores(scorecardData.OpponentBatsmen);
                        List<string> opponentBowlersErrorMessages = opponentFixturePlayer.ValidateOpponentBowlersScores(scorecardData.OpponentBowlers);
                        List<string> opponentOtherDetailsErrorMessages = opponentOtherFixtureDetail.ValidateOpponentOtherDetails(scorecardData.OpponentOtherDetail);
                        List<string> scorecardClubErrorMessages = scorecardData.ValidateClubScorecardTotals(scorecardData);
                        List<string> scorecardOpponentErrorMessages = scorecardData.ValidateOpponentScorecardTotals(scorecardData);

                        if ((clubBatsmenErrorMessages.Count() == 0) && (clubBowlersErrorMessages.Count() == 0) && (opponentBatsmenErrorMessages.Count() == 0) && (opponentBowlersErrorMessages.Count() == 0) && (clubOtherDetailsErrorMessages.Count() == 0) && (opponentOtherDetailsErrorMessages.Count() == 0) && (scorecardClubErrorMessages.Count() == 0) && (scorecardOpponentErrorMessages.Count() == 0))
                        {
                            //update

                            clubFixturePlayer.EditClubFixtureBatsmen(scorecardData.ClubBatsmen);
                            clubFixturePlayer.EditClubFixtureBowlers(scorecardData.ClubBowlers);
                            opponentFixturePlayer.EditOpponentFixtureBatsmen(scorecardData.OpponentBatsmen);
                            opponentFixturePlayer.EditOpponentFixtureBowlers(scorecardData.OpponentBowlers);
                            clubOtherFixtureDetail.EditClubOtherFixtureDetail(scorecardData.ClubOtherDetail);
                            opponentOtherFixtureDetail.EditOpponentOtherFixtureDetail(scorecardData.OpponentOtherDetail);

                            //create view model

                            ScorecardAbandonedFixtureScorecardBatAndBowlBattedFirst scorecard = new ScorecardAbandonedFixtureScorecardBatAndBowlBattedFirst();

                            scorecard.ScorecardFixture = fixtureRepository.GetFixture(scorecardData.ScorecardFixture.fixture_id);
                            scorecard.ScorecardHeaderDetail = scorecardHeaderRepository.GetScorecardHeader(scorecard.ScorecardFixture.fixture_id);
                            scorecard.ClubBatsmen = clubFixturePlayerRepository.GetClubBatsmen(scorecard.ScorecardFixture);
                            scorecard.ClubBowlers = clubFixturePlayerRepository.GetClubBowlers(scorecard.ScorecardFixture);
                            scorecard.ClubOtherDetail = clubOtherFixtureDetailRepository.GetClubOtherFixtureDetail(scorecard.ScorecardFixture.fixture_id);
                            scorecard.OpponentBatsmen = opponentFixturePlayerRepository.GetOpponentBatsmen(scorecard.ScorecardFixture);
                            scorecard.OpponentBowlers = opponentFixturePlayerRepository.GetOpponentBowlers(scorecard.ScorecardFixture);
                            scorecard.OpponentOtherDetail = opponentOtherFixtureDetailRepository.GetOpponentOtherFixtureDetail(scorecard.ScorecardFixture);
                            scorecard.TotalClubBatsmenRuns = clubFixturePlayer.GetTotalBatsmenRuns(scorecard.ClubBatsmen);
                            scorecard.TotalClubExtras = clubOtherFixtureDetailRepository.GetTotalClubExtras(scorecardData.ClubOtherDetail);
                            scorecard.TotalOpponentBatsmenRuns = opponentFixturePlayer.GetTotalBatsmenRuns(scorecard.OpponentBatsmen);
                            scorecard.TotalOpponentExtras = opponentOtherFixtureDetailRepository.GetTotalOpponentExtras(scorecardData.OpponentOtherDetail);

                            // update playerstatistics

                            playerSeasonStatistic.ResetStatistics();
                            playerSeasonStatistic.RegenerateStatistics();

                            return View("DisplayEditedAbandonedFixtureScorecardBatAndBowlBattedSecond", scorecard);
                        }
                        else
                        {

                            ScorecardAbandonedFixtureReInputScoresBatAndBowlViewModel reInputData = new ScorecardAbandonedFixtureReInputScoresBatAndBowlViewModel();

                            reInputData.ScorecardFixture = scorecardData.ScorecardFixture;
                            reInputData.ScorecardHeaderDetail = scorecardData.ScorecardHeaderDetail;
                            reInputData.ClubBatsmen = scorecardData.ClubBatsmen;
                            reInputData.OpponentBatsmen = scorecardData.OpponentBatsmen;
                            reInputData.ClubBowlers = scorecardData.ClubBowlers;
                            reInputData.OpponentBowlers = scorecardData.OpponentBowlers;
                            reInputData.ClubOtherDetail = scorecardData.ClubOtherDetail;
                            reInputData.OpponentOtherDetail = scorecardData.OpponentOtherDetail;
                            reInputData.DismissalTypes = db.DismissalTypes.ToList();
                            reInputData.ClubBatsmenErrorMessages = clubBatsmenErrorMessages;
                            reInputData.ClubBowlersErrorMessages = clubBowlersErrorMessages;
                            reInputData.OpponentBatsmenErrorMessages = opponentBatsmenErrorMessages;
                            reInputData.OpponentBowlersErrorMessages = opponentBowlersErrorMessages;
                            reInputData.ClubOtherDetailErrorMessages = clubOtherDetailsErrorMessages;
                            reInputData.OpponentOtherDetailErrorMessages = opponentOtherDetailsErrorMessages;
                            reInputData.ScorecardClubErrorMessages = scorecardClubErrorMessages;
                            reInputData.ScorecardOpponentErrorMessages = scorecardOpponentErrorMessages;
                            reInputData.TotalClubBatsmenRuns = clubFixturePlayer.GetTotalBatsmenRuns(reInputData.ClubBatsmen);
                            reInputData.TotalClubExtras = clubOtherFixtureDetail.GetTotalExtras(reInputData.ClubOtherDetail);
                            reInputData.TotalClubWicketsLost = clubFixturePlayer.GetTotalWicketsLost(reInputData.ClubBatsmen);
                            reInputData.TotalClubScore = reInputData.TotalClubBatsmenRuns + reInputData.TotalClubExtras;
                            reInputData.TotalOpponentBatsmenRuns = opponentFixturePlayer.GetTotalBatsmenRuns(reInputData.OpponentBatsmen);
                            reInputData.TotalOpponentExtras = opponentOtherFixtureDetail.GetTotalExtras(reInputData.OpponentOtherDetail);
                            reInputData.TotalOpponentWicketsLost = opponentFixturePlayer.GetTotalWicketsLost(reInputData.OpponentBatsmen);
                            reInputData.TotalOpponentScore = reInputData.TotalOpponentBatsmenRuns + reInputData.TotalOpponentExtras;

                            return View("ReInputEditAbandonedFixtureScoresBatAndBowlBattedSecond", reInputData);
                        }
                    }
                }

                if ((scorecardHeader.batted) && (!scorecardHeader.bowled))
                {
                    ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel scorecardData = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ParseFormCollectionBattedOnly(formCollection);

                    // Validate Input

                    List<string> clubBatsmenErrorMessages = clubFixturePlayer.ValidateClubBatsmenScores(scorecardData.ClubBatsmen);
                    List<string> clubOtherDetailsErrorMessages = clubOtherFixtureDetail.ValidateClubOtherDetails(scorecardData.ClubOtherDetail);
                    List<string> opponentBowlersErrorMessages = opponentFixturePlayer.ValidateOpponentBowlersScores(scorecardData.OpponentBowlers);
                    List<string> scorecardClubErrorMessages = scorecardData.ValidateClubScorecardTotals(scorecardData);

                    if ((clubBatsmenErrorMessages.Count() == 0) && (clubOtherDetailsErrorMessages.Count() == 0) && (opponentBowlersErrorMessages.Count() == 0) && (scorecardClubErrorMessages.Count() == 0))
                    {
                        //update

                        clubFixturePlayer.EditClubFixtureBatsmen(scorecardData.ClubBatsmen);
                        opponentFixturePlayer.EditOpponentFixtureBatsmen(scorecardData.OpponentBatsmen);
                        opponentFixturePlayer.EditOpponentFixtureBowlers(scorecardData.OpponentBowlers);
                        clubOtherFixtureDetail.EditClubOtherFixtureDetail(scorecardData.ClubOtherDetail);
                        
                        //create view model

                        ScorecardAbandonedFixtureScorecardBatAndBowlBattedFirst scorecard = new ScorecardAbandonedFixtureScorecardBatAndBowlBattedFirst();

                        scorecard.ScorecardFixture = fixtureRepository.GetFixture(scorecardData.ScorecardFixture.fixture_id);
                        scorecard.ScorecardHeaderDetail = scorecardHeaderRepository.GetScorecardHeader(scorecard.ScorecardFixture.fixture_id);
                        scorecard.ClubBatsmen = clubFixturePlayerRepository.GetClubBatsmen(scorecard.ScorecardFixture);
                        scorecard.ClubOtherDetail = clubOtherFixtureDetailRepository.GetClubOtherFixtureDetail(scorecard.ScorecardFixture.fixture_id);
                        scorecard.OpponentBatsmen = opponentFixturePlayerRepository.GetOpponentBatsmen(scorecard.ScorecardFixture);
                        scorecard.OpponentBowlers = opponentFixturePlayerRepository.GetOpponentBowlers(scorecard.ScorecardFixture);
                        scorecard.TotalClubBatsmenRuns = clubFixturePlayer.GetTotalBatsmenRuns(scorecard.ClubBatsmen);
                        scorecard.TotalClubExtras = clubOtherFixtureDetailRepository.GetTotalClubExtras(scorecardData.ClubOtherDetail);

                        // update playerstatistics

                        playerSeasonStatistic.ResetStatistics();
                        playerSeasonStatistic.RegenerateStatistics();

                        return View("DisplayEditedAbandonedFixtureScorecardBattedOnly", scorecard);
                    }
                    else
                    {

                        ScorecardAbandonedFixtureReInputScoresBatAndBowlViewModel reInputData = new ScorecardAbandonedFixtureReInputScoresBatAndBowlViewModel();

                        reInputData.ScorecardFixture = scorecardData.ScorecardFixture;
                        reInputData.ScorecardHeaderDetail = scorecardData.ScorecardHeaderDetail;
                        reInputData.ClubBatsmen = scorecardData.ClubBatsmen;
                        reInputData.OpponentBatsmen = scorecardData.OpponentBatsmen;
                        reInputData.ClubBowlers = scorecardData.ClubBowlers;
                        reInputData.OpponentBowlers = scorecardData.OpponentBowlers;
                        reInputData.ClubOtherDetail = scorecardData.ClubOtherDetail;
                        reInputData.OpponentOtherDetail = scorecardData.OpponentOtherDetail;
                        reInputData.DismissalTypes = db.DismissalTypes.ToList();
                        reInputData.ClubBatsmenErrorMessages = clubBatsmenErrorMessages;
                        reInputData.OpponentBowlersErrorMessages = opponentBowlersErrorMessages;
                        reInputData.ClubOtherDetailErrorMessages = clubOtherDetailsErrorMessages;
                        reInputData.ScorecardClubErrorMessages = scorecardClubErrorMessages;
                        reInputData.TotalClubBatsmenRuns = clubFixturePlayer.GetTotalBatsmenRuns(reInputData.ClubBatsmen);
                        reInputData.TotalClubExtras = clubOtherFixtureDetail.GetTotalExtras(reInputData.ClubOtherDetail);
                        reInputData.TotalClubWicketsLost = clubFixturePlayer.GetTotalWicketsLost(reInputData.ClubBatsmen);
                        reInputData.TotalClubScore = reInputData.TotalClubBatsmenRuns + reInputData.TotalClubExtras;

                        return View("ReInputEditAbandonedFixtureScoresBattedOnly", reInputData);
                    }
                }

                if ((!scorecardHeader.batted) && (scorecardHeader.bowled))
                {
                    ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel scorecardData = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ParseFormCollectionBowledOnly(formCollection);

                    // Validate Input

                    List<string> clubBowlersErrorMessages = clubFixturePlayer.ValidateClubBowlersScores(scorecardData.ClubBowlers);
                    List<string> opponentBatsmenErrorMessages = opponentFixturePlayer.ValidateOpponentBatsmenScores(scorecardData.OpponentBatsmen);
                    List<string> opponentOtherDetailsErrorMessages = opponentOtherFixtureDetail.ValidateOpponentOtherDetails(scorecardData.OpponentOtherDetail);
                    List<string> scorecardOpponentErrorMessages = scorecardData.ValidateOpponentScorecardTotals(scorecardData);

                    if ((clubBowlersErrorMessages.Count() == 0) && (opponentBatsmenErrorMessages.Count() == 0) && (opponentOtherDetailsErrorMessages.Count() == 0) && (scorecardOpponentErrorMessages.Count() == 0))
                    {
                        //update

                        clubFixturePlayer.EditClubFixtureBatsmen(scorecardData.ClubBatsmen);
                        clubFixturePlayer.EditClubFixtureBowlers(scorecardData.ClubBowlers);
                        opponentFixturePlayer.EditOpponentFixtureBatsmen(scorecardData.OpponentBatsmen);
                        opponentOtherFixtureDetail.EditOpponentOtherFixtureDetail(scorecardData.OpponentOtherDetail);
                        
                        //create view model

                        ScorecardAbandonedFixtureScorecardBatAndBowlBattedFirst scorecard = new ScorecardAbandonedFixtureScorecardBatAndBowlBattedFirst();

                        scorecard.ScorecardFixture = fixtureRepository.GetFixture(scorecardData.ScorecardFixture.fixture_id);
                        scorecard.ScorecardHeaderDetail = scorecardHeaderRepository.GetScorecardHeader(scorecard.ScorecardFixture.fixture_id);
                        scorecard.ClubBatsmen = clubFixturePlayerRepository.GetClubBatsmen(scorecard.ScorecardFixture);
                        scorecard.ClubBowlers = clubFixturePlayerRepository.GetClubBowlers(scorecard.ScorecardFixture);
                        scorecard.OpponentBatsmen = opponentFixturePlayerRepository.GetOpponentBatsmen(scorecard.ScorecardFixture);
                        scorecard.OpponentOtherDetail = opponentOtherFixtureDetailRepository.GetOpponentOtherFixtureDetail(scorecard.ScorecardFixture);
                        scorecard.TotalOpponentExtras = opponentOtherFixtureDetailRepository.GetTotalOpponentExtras(scorecardData.OpponentOtherDetail);
                        scorecard.TotalOpponentBatsmenRuns = opponentFixturePlayer.GetTotalBatsmenRuns(scorecard.OpponentBatsmen);
                        scorecard.TotalOpponentExtras = opponentOtherFixtureDetailRepository.GetTotalOpponentExtras(scorecardData.OpponentOtherDetail);

                        // update playerstatistics

                        playerSeasonStatistic.ResetStatistics();
                        playerSeasonStatistic.RegenerateStatistics();

                        return View("DisplayEditedAbandonedFixtureScorecardBowledOnly", scorecard);
                    }
                    else
                    {
                        ScorecardAbandonedFixtureReInputScoresBatAndBowlViewModel reInputData = new ScorecardAbandonedFixtureReInputScoresBatAndBowlViewModel();

                        reInputData.ScorecardFixture = scorecardData.ScorecardFixture;
                        reInputData.ScorecardHeaderDetail = scorecardData.ScorecardHeaderDetail;
                        reInputData.ClubBatsmen = scorecardData.ClubBatsmen;
                        reInputData.OpponentBatsmen = scorecardData.OpponentBatsmen;
                        reInputData.ClubBowlers = scorecardData.ClubBowlers;
                        reInputData.OpponentBowlers = scorecardData.OpponentBowlers;
                        reInputData.OpponentOtherDetail = scorecardData.OpponentOtherDetail;
                        reInputData.DismissalTypes = db.DismissalTypes.ToList();
                        reInputData.ClubBowlersErrorMessages = clubBowlersErrorMessages;
                        reInputData.OpponentBatsmenErrorMessages = opponentBatsmenErrorMessages;
                        reInputData.OpponentOtherDetailErrorMessages = opponentOtherDetailsErrorMessages;
                        reInputData.ScorecardOpponentErrorMessages = scorecardOpponentErrorMessages;
                        reInputData.TotalClubBatsmenRuns = clubFixturePlayer.GetTotalBatsmenRuns(reInputData.ClubBatsmen);
                        reInputData.TotalOpponentBatsmenRuns = opponentFixturePlayer.GetTotalBatsmenRuns(reInputData.OpponentBatsmen);
                        reInputData.TotalOpponentExtras = opponentOtherFixtureDetail.GetTotalExtras(reInputData.OpponentOtherDetail);
                        reInputData.TotalOpponentWicketsLost = opponentFixturePlayer.GetTotalWicketsLost(reInputData.OpponentBatsmen);
                        reInputData.TotalOpponentScore = reInputData.TotalOpponentBatsmenRuns + reInputData.TotalOpponentExtras;

                        return View("ReInputEditAbandonedFixtureScoresBowledOnly", reInputData);
                    }
                }

                if (scorecardHeader.result_id == 3)
                {
                    if (scorecardHeader.batted_first)
                    {
                        ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel scorecardData = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ParseFormCollectionBattedFirst(formCollection);

                        // Validate Input

                        List<string> clubBatsmenErrorMessages = clubFixturePlayer.ValidateClubBatsmenScores(scorecardData.ClubBatsmen);
                        List<string> clubBowlersErrorMessages = clubFixturePlayer.ValidateClubBowlersScores(scorecardData.ClubBowlers);
                        List<string> clubOtherDetailsErrorMessages = clubOtherFixtureDetail.ValidateClubOtherDetails(scorecardData.ClubOtherDetail);
                        List<string> opponentBatsmenErrorMessages = opponentFixturePlayer.ValidateOpponentBatsmenScores(scorecardData.OpponentBatsmen);
                        List<string> opponentBowlersErrorMessages = opponentFixturePlayer.ValidateOpponentBowlersScores(scorecardData.OpponentBowlers);
                        List<string> opponentOtherDetailsErrorMessages = opponentOtherFixtureDetail.ValidateOpponentOtherDetails(scorecardData.OpponentOtherDetail);
                        List<string> scorecardClubErrorMessages = scorecardData.ValidateClubScorecardTotals(scorecardData);
                        List<string> scorecardOpponentErrorMessages = scorecardData.ValidateOpponentScorecardTotals(scorecardData);

                        if ((clubBatsmenErrorMessages.Count() == 0) && (clubBowlersErrorMessages.Count() == 0) && (opponentBatsmenErrorMessages.Count() == 0) && (opponentBowlersErrorMessages.Count() == 0) && (clubOtherDetailsErrorMessages.Count() == 0) && (opponentOtherDetailsErrorMessages.Count() == 0) && (scorecardClubErrorMessages.Count() == 0) && (scorecardOpponentErrorMessages.Count() == 0))
                        {
                            //update

                            clubFixturePlayer.EditClubFixtureBatsmen(scorecardData.ClubBatsmen);
                            clubFixturePlayer.EditClubFixtureBowlers(scorecardData.ClubBowlers);
                            opponentFixturePlayer.EditOpponentFixtureBatsmen(scorecardData.OpponentBatsmen);
                            opponentFixturePlayer.EditOpponentFixtureBowlers(scorecardData.OpponentBowlers);
                            clubOtherFixtureDetail.EditClubOtherFixtureDetail(scorecardData.ClubOtherDetail);
                            opponentOtherFixtureDetail.EditOpponentOtherFixtureDetail(scorecardData.OpponentOtherDetail);
                            
                            //create view model

                            ScorecardAbandonedFixtureScorecardBatAndBowlBattedFirst scorecard = new ScorecardAbandonedFixtureScorecardBatAndBowlBattedFirst();

                            scorecard.ScorecardFixture = fixtureRepository.GetFixture(scorecardData.ScorecardFixture.fixture_id);
                            scorecard.ScorecardHeaderDetail = scorecardHeaderRepository.GetScorecardHeader(scorecard.ScorecardFixture.fixture_id);
                            scorecard.ClubBatsmen = clubFixturePlayerRepository.GetClubBatsmen(scorecard.ScorecardFixture);
                            scorecard.ClubBowlers = clubFixturePlayerRepository.GetClubBowlers(scorecard.ScorecardFixture);
                            scorecard.ClubOtherDetail = clubOtherFixtureDetailRepository.GetClubOtherFixtureDetail(scorecard.ScorecardFixture.fixture_id);
                            scorecard.OpponentBatsmen = opponentFixturePlayerRepository.GetOpponentBatsmen(scorecard.ScorecardFixture);
                            scorecard.OpponentBowlers = opponentFixturePlayerRepository.GetOpponentBowlers(scorecard.ScorecardFixture);
                            scorecard.OpponentOtherDetail = opponentOtherFixtureDetailRepository.GetOpponentOtherFixtureDetail(scorecard.ScorecardFixture);
                            scorecard.TotalClubBatsmenRuns = clubFixturePlayer.GetTotalBatsmenRuns(scorecard.ClubBatsmen);
                            scorecard.TotalClubExtras = clubOtherFixtureDetailRepository.GetTotalClubExtras(scorecardData.ClubOtherDetail);
                            scorecard.TotalOpponentBatsmenRuns = opponentFixturePlayer.GetTotalBatsmenRuns(scorecard.OpponentBatsmen);
                            scorecard.TotalOpponentExtras = opponentOtherFixtureDetailRepository.GetTotalOpponentExtras(scorecardData.OpponentOtherDetail);

                            // update playerstatistics

                            playerSeasonStatistic.ResetStatistics();
                            playerSeasonStatistic.RegenerateStatistics();

                            return View("DisplayEditedCompletedFixtureScorecardBattedFirst", scorecard);
                        }
                        else
                        {
                            ScorecardAbandonedFixtureReInputScoresBatAndBowlViewModel reInputData = new ScorecardAbandonedFixtureReInputScoresBatAndBowlViewModel();

                            reInputData.ScorecardFixture = scorecardData.ScorecardFixture;
                            reInputData.ScorecardHeaderDetail = scorecardData.ScorecardHeaderDetail;
                            reInputData.ClubBatsmen = scorecardData.ClubBatsmen;
                            reInputData.OpponentBatsmen = scorecardData.OpponentBatsmen;
                            reInputData.ClubBowlers = scorecardData.ClubBowlers;
                            reInputData.OpponentBowlers = scorecardData.OpponentBowlers;
                            reInputData.ClubOtherDetail = scorecardData.ClubOtherDetail;
                            reInputData.OpponentOtherDetail = scorecardData.OpponentOtherDetail;
                            reInputData.DismissalTypes = db.DismissalTypes.ToList();
                            reInputData.ClubBatsmenErrorMessages = clubBatsmenErrorMessages;
                            reInputData.ClubBowlersErrorMessages = clubBowlersErrorMessages;
                            reInputData.OpponentBatsmenErrorMessages = opponentBatsmenErrorMessages;
                            reInputData.OpponentBowlersErrorMessages = opponentBowlersErrorMessages;
                            reInputData.ClubOtherDetailErrorMessages = clubOtherDetailsErrorMessages;
                            reInputData.OpponentOtherDetailErrorMessages = opponentOtherDetailsErrorMessages;
                            reInputData.ScorecardClubErrorMessages = scorecardClubErrorMessages;
                            reInputData.ScorecardOpponentErrorMessages = scorecardOpponentErrorMessages;
                            reInputData.TotalClubBatsmenRuns = clubFixturePlayer.GetTotalBatsmenRuns(reInputData.ClubBatsmen);
                            reInputData.TotalClubExtras = clubOtherFixtureDetail.GetTotalExtras(reInputData.ClubOtherDetail);
                            reInputData.TotalClubWicketsLost = clubFixturePlayer.GetTotalWicketsLost(reInputData.ClubBatsmen);
                            reInputData.TotalClubScore = reInputData.TotalClubBatsmenRuns + reInputData.TotalClubExtras;
                            reInputData.TotalOpponentBatsmenRuns = opponentFixturePlayer.GetTotalBatsmenRuns(reInputData.OpponentBatsmen);
                            reInputData.TotalOpponentExtras = opponentOtherFixtureDetail.GetTotalExtras(reInputData.OpponentOtherDetail);
                            reInputData.TotalOpponentWicketsLost = opponentFixturePlayer.GetTotalWicketsLost(reInputData.OpponentBatsmen);
                            reInputData.TotalOpponentScore = reInputData.TotalOpponentBatsmenRuns + reInputData.TotalOpponentExtras;

                            return View("ReInputEditCompletedFixtureScoresBattedFirst", reInputData);
                        }
                    }
                }

                if (!scorecardHeader.batted_first)
                {
                    ScorecardAbandonedFixtureEnterScoresBatAndBowlViewModel scorecardData = scorecardAbandonedFixtureEnterScoresBatAndBowlViewModel.ParseFormCollectionBattedSecond(formCollection);

                    List<string> clubBatsmenErrorMessages = clubFixturePlayer.ValidateClubBatsmenScores(scorecardData.ClubBatsmen);
                    List<string> clubBowlersErrorMessages = clubFixturePlayer.ValidateClubBowlersScores(scorecardData.ClubBowlers);
                    List<string> clubOtherDetailsErrorMessages = clubOtherFixtureDetail.ValidateClubOtherDetails(scorecardData.ClubOtherDetail);
                    List<string> opponentBatsmenErrorMessages = opponentFixturePlayer.ValidateOpponentBatsmenScores(scorecardData.OpponentBatsmen);
                    List<string> opponentBowlersErrorMessages = opponentFixturePlayer.ValidateOpponentBowlersScores(scorecardData.OpponentBowlers);
                    List<string> opponentOtherDetailsErrorMessages = opponentOtherFixtureDetail.ValidateOpponentOtherDetails(scorecardData.OpponentOtherDetail);
                    List<string> scorecardClubErrorMessages = scorecardData.ValidateClubScorecardTotals(scorecardData);
                    List<string> scorecardOpponentErrorMessages = scorecardData.ValidateOpponentScorecardTotals(scorecardData);

                    if ((clubBatsmenErrorMessages.Count() == 0) && (clubBowlersErrorMessages.Count() == 0) && (opponentBatsmenErrorMessages.Count() == 0) && (opponentBowlersErrorMessages.Count() == 0) && (clubOtherDetailsErrorMessages.Count() == 0) && (opponentOtherDetailsErrorMessages.Count() == 0) && (scorecardClubErrorMessages.Count() == 0) && (scorecardOpponentErrorMessages.Count() == 0))
                    {
                        //update

                        clubFixturePlayer.EditClubFixtureBatsmen(scorecardData.ClubBatsmen);
                        clubFixturePlayer.EditClubFixtureBowlers(scorecardData.ClubBowlers);
                        opponentFixturePlayer.EditOpponentFixtureBatsmen(scorecardData.OpponentBatsmen);
                        opponentFixturePlayer.EditOpponentFixtureBowlers(scorecardData.OpponentBowlers);
                        clubOtherFixtureDetail.EditClubOtherFixtureDetail(scorecardData.ClubOtherDetail);
                        opponentOtherFixtureDetail.EditOpponentOtherFixtureDetail(scorecardData.OpponentOtherDetail);
                        
                        //create view model

                        ScorecardAbandonedFixtureScorecardBatAndBowlBattedFirst scorecard = new ScorecardAbandonedFixtureScorecardBatAndBowlBattedFirst();

                        scorecard.ScorecardFixture = fixtureRepository.GetFixture(scorecardData.ScorecardFixture.fixture_id);
                        scorecard.ScorecardHeaderDetail = scorecardHeaderRepository.GetScorecardHeader(scorecard.ScorecardFixture.fixture_id);
                        scorecard.ClubBatsmen = clubFixturePlayerRepository.GetClubBatsmen(scorecard.ScorecardFixture);
                        scorecard.ClubBowlers = clubFixturePlayerRepository.GetClubBowlers(scorecard.ScorecardFixture);
                        scorecard.ClubOtherDetail = clubOtherFixtureDetailRepository.GetClubOtherFixtureDetail(scorecard.ScorecardFixture.fixture_id);
                        scorecard.OpponentBatsmen = opponentFixturePlayerRepository.GetOpponentBatsmen(scorecard.ScorecardFixture);
                        scorecard.OpponentBowlers = opponentFixturePlayerRepository.GetOpponentBowlers(scorecard.ScorecardFixture);
                        scorecard.OpponentOtherDetail = opponentOtherFixtureDetailRepository.GetOpponentOtherFixtureDetail(scorecard.ScorecardFixture);
                        scorecard.TotalClubBatsmenRuns = clubFixturePlayer.GetTotalBatsmenRuns(scorecard.ClubBatsmen);
                        scorecard.TotalClubExtras = clubOtherFixtureDetailRepository.GetTotalClubExtras(scorecardData.ClubOtherDetail);
                        scorecard.TotalOpponentBatsmenRuns = opponentFixturePlayer.GetTotalBatsmenRuns(scorecard.OpponentBatsmen);
                        scorecard.TotalOpponentExtras = opponentOtherFixtureDetailRepository.GetTotalOpponentExtras(scorecardData.OpponentOtherDetail);

                        // update playerstatistics

                        playerSeasonStatistic.ResetStatistics();
                        playerSeasonStatistic.RegenerateStatistics();

                        return View("DisplayEditedCompletedFixtureScorecardBattedSecond", scorecard);
                    }
                    else
                    {

                        ScorecardAbandonedFixtureReInputScoresBatAndBowlViewModel reInputData = new ScorecardAbandonedFixtureReInputScoresBatAndBowlViewModel();

                        reInputData.ScorecardFixture = scorecardData.ScorecardFixture;
                        reInputData.ScorecardHeaderDetail = scorecardData.ScorecardHeaderDetail;
                        reInputData.ClubBatsmen = scorecardData.ClubBatsmen;
                        reInputData.OpponentBatsmen = scorecardData.OpponentBatsmen;
                        reInputData.ClubBowlers = scorecardData.ClubBowlers;
                        reInputData.OpponentBowlers = scorecardData.OpponentBowlers;
                        reInputData.ClubOtherDetail = scorecardData.ClubOtherDetail;
                        reInputData.OpponentOtherDetail = scorecardData.OpponentOtherDetail;
                        reInputData.DismissalTypes = db.DismissalTypes.ToList();
                        reInputData.ClubBatsmenErrorMessages = clubBatsmenErrorMessages;
                        reInputData.ClubBowlersErrorMessages = clubBowlersErrorMessages;
                        reInputData.OpponentBatsmenErrorMessages = opponentBatsmenErrorMessages;
                        reInputData.OpponentBowlersErrorMessages = opponentBowlersErrorMessages;
                        reInputData.ClubOtherDetailErrorMessages = clubOtherDetailsErrorMessages;
                        reInputData.OpponentOtherDetailErrorMessages = opponentOtherDetailsErrorMessages;
                        reInputData.ScorecardClubErrorMessages = scorecardClubErrorMessages;
                        reInputData.ScorecardOpponentErrorMessages = scorecardOpponentErrorMessages;
                        reInputData.TotalClubBatsmenRuns = clubFixturePlayer.GetTotalBatsmenRuns(reInputData.ClubBatsmen);
                        reInputData.TotalClubExtras = clubOtherFixtureDetail.GetTotalExtras(reInputData.ClubOtherDetail);
                        reInputData.TotalClubWicketsLost = clubFixturePlayer.GetTotalWicketsLost(reInputData.ClubBatsmen);
                        reInputData.TotalClubScore = reInputData.TotalClubBatsmenRuns + reInputData.TotalClubExtras;
                        reInputData.TotalOpponentBatsmenRuns = opponentFixturePlayer.GetTotalBatsmenRuns(reInputData.OpponentBatsmen);
                        reInputData.TotalOpponentExtras = opponentOtherFixtureDetail.GetTotalExtras(reInputData.OpponentOtherDetail);
                        reInputData.TotalOpponentWicketsLost = opponentFixturePlayer.GetTotalWicketsLost(reInputData.OpponentBatsmen);
                        reInputData.TotalOpponentScore = reInputData.TotalOpponentBatsmenRuns + reInputData.TotalOpponentExtras;

                        return View("ReInputEditCompletedFixtureScoresBattedSecond", reInputData);
                    }
                }
            }
            return View();
        }

        //
        // POST: scorecard/DeleteScorecard

        [HttpPost]
        public ActionResult DeleteScorecard(FormCollection formCollection)
        {

            if (ModelState.IsValid)
            {
                Fixture fixture = fixtureRepository.ParseScorecardFormCollection(formCollection);
                ScorecardHeader scorecardHeader = scorecardHeaderRepository.ParseScorecardFormCollection(formCollection);


                clubFixturePlayer.ResetClubFixturePlayerDetails(fixture.fixture_id);
                opponentFixturePlayer.DeleteOpponentFixturePlayers(fixture.fixture_id);
                if (scorecardHeader.batted)
                {
                    clubOtherFixtureDetail.DeleteClubOtherFixtureDetails(fixture.fixture_id);
                }
                if (scorecardHeader.bowled)
                {
                    opponentOtherFixtureDetail.DeleteOpponentOtherFixtureDetails(fixture.fixture_id);
                }
                scorecardHeader.DeleteFixtureHeaderDetails(fixture.fixture_id);

                playerSeasonStatistic.ResetStatistics();
                playerSeasonStatistic.RegenerateStatistics();

                return View("DeleteConfirmed", fixture);
            }
            return View();
        }

        //
        // GET: Scorecard/GetScorecardFixtures

        public ActionResult GetScorecardFixtures()
        {
            var fixtures = fixtureRepository.FindFixtureScorecards();

            if (fixtures.Count() < 1)
            {
                ViewBag.Message = "There are no fixtures with scorecards to be entered";
            }

            return View(fixtures.ToList());
        }
                
        //
        //  GET: Scorecard/GetScorecard

        public ActionResult GetScorecard(int id)
        {
            ScorecardBatAndBowlViewModel scorecardBatAndBowlViewModel = new ScorecardBatAndBowlViewModel();
            scorecardBatAndBowlViewModel.ScorecardFixture = fixtureRepository.GetFixture(id);
            scorecardBatAndBowlViewModel.ScorecardHeaderDetail = scorecardHeaderRepository.GetScorecardHeader(scorecardBatAndBowlViewModel.ScorecardFixture);

            if (scorecardBatAndBowlViewModel.ScorecardHeaderDetail.result_id == 1)                           // called off fixtures
            {
                return View();
            }

            if (scorecardBatAndBowlViewModel.ScorecardHeaderDetail.result_id == 2)                           // abandoned fixtures
            {
                if ((scorecardBatAndBowlViewModel.ScorecardHeaderDetail.batted) && (scorecardBatAndBowlViewModel.ScorecardHeaderDetail.bowled))
                {
                    scorecardBatAndBowlViewModel.ClubBatsmen = clubFixturePlayerRepository.GetClubBatsmen(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.ClubBowlers = clubFixturePlayerRepository.GetClubBowlers(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.ClubOtherDetail = clubOtherFixtureDetailRepository.GetClubOtherFixtureDetail(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.TotalClubBatsmenRuns = clubFixturePlayer.GetTotalBatsmenRuns(scorecardBatAndBowlViewModel.ClubBatsmen);
                    scorecardBatAndBowlViewModel.TotalClubExtras = clubOtherFixtureDetailRepository.GetTotalClubExtras(scorecardBatAndBowlViewModel.ClubOtherDetail);
                    scorecardBatAndBowlViewModel.OpponentBatsmen = opponentFixturePlayerRepository.GetOpponentBatsmen(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.OpponentBowlers = opponentFixturePlayerRepository.GetOpponentBowlers(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.OpponentOtherDetail = opponentOtherFixtureDetailRepository.GetOpponentOtherFixtureDetail(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.TotalOpponentBatsmenRuns = opponentFixturePlayer.GetTotalBatsmenRuns(scorecardBatAndBowlViewModel.OpponentBatsmen);
                    scorecardBatAndBowlViewModel.TotalOpponentExtras = opponentOtherFixtureDetailRepository.GetTotalOpponentExtras(scorecardBatAndBowlViewModel.OpponentOtherDetail);
                    scorecardBatAndBowlViewModel.DismissalTypes = db.DismissalTypes.ToList();

                    if (scorecardBatAndBowlViewModel.ScorecardHeaderDetail.batted_first)
                    {
                        return View("DisplayAbandonedScorecardBatAndBowlBattedFirst", scorecardBatAndBowlViewModel);
                    }
                    if (!scorecardBatAndBowlViewModel.ScorecardHeaderDetail.batted_first)
                    {
                        return View("DisplayAbandonedScorecardBatAndBowlBattedSecond", scorecardBatAndBowlViewModel);
                    }
                }

                if ((scorecardBatAndBowlViewModel.ScorecardHeaderDetail.batted) && (!scorecardBatAndBowlViewModel.ScorecardHeaderDetail.bowled))
                {
                    scorecardBatAndBowlViewModel.ClubBatsmen = clubFixturePlayerRepository.GetClubBatsmen(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.ClubOtherDetail = clubOtherFixtureDetailRepository.GetClubOtherFixtureDetail(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.TotalClubBatsmenRuns = clubFixturePlayer.GetTotalBatsmenRuns(scorecardBatAndBowlViewModel.ClubBatsmen);
                    scorecardBatAndBowlViewModel.TotalClubExtras = clubOtherFixtureDetailRepository.GetTotalClubExtras(scorecardBatAndBowlViewModel.ClubOtherDetail);
                    scorecardBatAndBowlViewModel.OpponentBatsmen = opponentFixturePlayerRepository.GetOpponentBatsmen(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.OpponentBowlers = opponentFixturePlayerRepository.GetOpponentBowlers(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.DismissalTypes = db.DismissalTypes.ToList();

                    return View("DisplayAbandonedScorecardBattedOnly", scorecardBatAndBowlViewModel);
                }
                if ((!scorecardBatAndBowlViewModel.ScorecardHeaderDetail.batted) && (scorecardBatAndBowlViewModel.ScorecardHeaderDetail.bowled))
                {
                    scorecardBatAndBowlViewModel.ClubBatsmen = clubFixturePlayerRepository.GetClubBatsmen(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.ClubBowlers = clubFixturePlayerRepository.GetClubBowlers(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.OpponentBatsmen = opponentFixturePlayerRepository.GetOpponentBatsmen(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.OpponentOtherDetail = opponentOtherFixtureDetailRepository.GetOpponentOtherFixtureDetail(scorecardBatAndBowlViewModel.ScorecardFixture);
                    scorecardBatAndBowlViewModel.TotalOpponentBatsmenRuns = opponentFixturePlayer.GetTotalBatsmenRuns(scorecardBatAndBowlViewModel.OpponentBatsmen);
                    scorecardBatAndBowlViewModel.TotalOpponentExtras = opponentOtherFixtureDetailRepository.GetTotalOpponentExtras(scorecardBatAndBowlViewModel.OpponentOtherDetail);
                    scorecardBatAndBowlViewModel.DismissalTypes = db.DismissalTypes.ToList();

                    return View("DisplayAbandonedScorecardBowledOnly", scorecardBatAndBowlViewModel);
                }
            }

            if (scorecardBatAndBowlViewModel.ScorecardHeaderDetail.result_id == 3)                           // completed fixtures
            {

                scorecardBatAndBowlViewModel.ClubBatsmen = clubFixturePlayerRepository.GetClubBatsmen(scorecardBatAndBowlViewModel.ScorecardFixture);
                scorecardBatAndBowlViewModel.ClubBowlers = clubFixturePlayerRepository.GetClubBowlers(scorecardBatAndBowlViewModel.ScorecardFixture);
                scorecardBatAndBowlViewModel.ClubOtherDetail = clubOtherFixtureDetailRepository.GetClubOtherFixtureDetail(scorecardBatAndBowlViewModel.ScorecardFixture);
                scorecardBatAndBowlViewModel.TotalClubBatsmenRuns = clubFixturePlayer.GetTotalBatsmenRuns(scorecardBatAndBowlViewModel.ClubBatsmen);
                scorecardBatAndBowlViewModel.TotalClubExtras = clubOtherFixtureDetailRepository.GetTotalClubExtras(scorecardBatAndBowlViewModel.ClubOtherDetail);
                scorecardBatAndBowlViewModel.OpponentBatsmen = opponentFixturePlayerRepository.GetOpponentBatsmen(scorecardBatAndBowlViewModel.ScorecardFixture);
                scorecardBatAndBowlViewModel.OpponentBowlers = opponentFixturePlayerRepository.GetOpponentBowlers(scorecardBatAndBowlViewModel.ScorecardFixture);
                scorecardBatAndBowlViewModel.OpponentOtherDetail = opponentOtherFixtureDetailRepository.GetOpponentOtherFixtureDetail(scorecardBatAndBowlViewModel.ScorecardFixture);
                scorecardBatAndBowlViewModel.TotalOpponentBatsmenRuns = opponentFixturePlayer.GetTotalBatsmenRuns(scorecardBatAndBowlViewModel.OpponentBatsmen);
                scorecardBatAndBowlViewModel.TotalOpponentExtras = opponentOtherFixtureDetailRepository.GetTotalOpponentExtras(scorecardBatAndBowlViewModel.OpponentOtherDetail);
                scorecardBatAndBowlViewModel.DismissalTypes = db.DismissalTypes.ToList();

                if (scorecardBatAndBowlViewModel.ScorecardHeaderDetail.batted_first)
                {
                    return View("DisplayCompletedFixtureScorecardBattedFirst", scorecardBatAndBowlViewModel);
                }
                if (!scorecardBatAndBowlViewModel.ScorecardHeaderDetail.batted_first)
                {
                    return View("DisplayCompletedFixtureScorecardBattedSecond", scorecardBatAndBowlViewModel);
                }
            }
            return View();
        }

    }
}